package com.yiqi.liebang.feature.order.view;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.EventEntity;
import com.yiqi.liebang.entity.bo.UnReadBo;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class OrderTopicFragment extends BaseFragment {

    @BindView(R.id.tl_2)
    SlidingTabLayout tabLayout_2;
    @BindView(R.id.vp)
    ViewPager mVp;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "进行中", "待评价", "已结束", "已收到"
    };
    private MyPagerAdapter mAdapter;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_topic_order;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    public void onStart() {
        super.onStart();
        RxBus.getDefault().register(this);

    }



//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void eventBus(EventEntity obj) {
//        switch (obj.what) {
//            case 12:
//                getUnReadNum();
//                break;
//        }
//    }

    @Override
    protected void initView() {
        for (int i = 0; i < mTitles.length; i++) {
            mFragments.add(TopicListFragment.getInstance(i+1));
        }
        mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mVp.setAdapter(mAdapter);

        tabLayout_2.setViewPager(mVp, mTitles, getActivity(), mFragments);

        getUnReadNum();

    }



    @Override
    protected void initData() {

    }

    private void getUnReadNum(){
        Logger.e("getUnReadNum----------->");
        AM.api().createApiService(HomeApi.class).getUnreadNUm(1)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<UnReadBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UnReadBo unReadBo) {
                        if (unReadBo == null) return;
                        if (tabLayout_2==null) return;

                        if (unReadBo.getUnreadNum1() > 0) {
                            tabLayout_2.showMsg(0, unReadBo.getUnreadNum1());
                            tabLayout_2.setMsgMargin(0, -39, 0);
                        }
                        if (unReadBo.getUnreadNum2() > 0) {
                            tabLayout_2.showMsg(1, unReadBo.getUnreadNum2());
                            tabLayout_2.setMsgMargin(1, -39, 0);
                        }
                        if (unReadBo.getUnreadNum3() > 0) {
                            tabLayout_2.showMsg(2, unReadBo.getUnreadNum3());
                            tabLayout_2.setMsgMargin(2, -39, 0);
                        }
                        if (unReadBo.getUnreadNum4() > 0) {
                            tabLayout_2.showMsg(3, unReadBo.getUnreadNum4());
                            tabLayout_2.setMsgMargin(3, -39, 0);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    protected void initEvent() {

    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }

}
